package com.resou.reader.mine.vipcenter.more;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.bookstore.model.PlateNovels;
import com.resou.reader.mine.vipcenter.adapter.MonthlyMoreAdapter;
import com.resou.reader.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 包月模块查看更多
 */
public class MonthlyMoreActivity extends ResouBaseActivity<MonthlyMorePresenter> implements IMonthlyMoreContract.IMonthlyMoreView {

    /**
     * 启动方法
     *
     * @param context
     * @param plateId
     * @param plateViewType
     */
    public static void start(Context context, String plateName, String plateId, String plateViewType) {
        Intent starter = new Intent(context, MonthlyMoreActivity.class);
        starter.putExtra("plateId", plateId);
        starter.putExtra("plateName", plateName);
        starter.putExtra("plateViewType", plateViewType);
        context.startActivity(starter);
    }

    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;

    MonthlyMoreAdapter monthlyMoreAdapter;
    int page = 0;
    String plateName;
    String plateId;
    String plateViewType;


    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        if (mPresenter == null) {
            mPresenter = new MonthlyMorePresenter(this);
        }
    }

    @Override
    protected void initView() {
        plateId = getIntent().getStringExtra("plateId");
        plateViewType = getIntent().getStringExtra("plateViewType");
        plateName = getIntent().getStringExtra("plateName");
        ClassicsFooter.REFRESH_FOOTER_NOTHING = "别拉了，小热也是有底线的啦~";
        tvTitle.setText(plateName + "");
        monthlyMoreAdapter = new MonthlyMoreAdapter(R.layout.adapter_monthlymore, null);
        monthlyMoreAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(monthlyMoreAdapter);
        mSmartRefreshLayout.setEnableFooterFollowWhenLoadFinished(true);
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.quearMonthlyMore(plateId, plateViewType, page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                mPresenter.quearMonthlyMore(plateId, plateViewType, page);
            }
        });
        monthlyMoreAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PlateNovels plateNovels = (PlateNovels) adapter.getData().get(position);
                BookDetailActivity.startDetailActivity(MonthlyMoreActivity.this, plateNovels.getNovelId());
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_monthly_more;
    }

    @Override
    public void retryRefreshData() {
        page = 0;
        mPresenter.quearMonthlyMore(plateId, plateViewType, page);
    }

    @Override
    public void initData() {
        page = 0;
        mPresenter.quearMonthlyMore(plateId, plateViewType, page);
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showContent();
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {

    }


    @OnClick(R.id.ivBack)
    public void onClick() {
        finish();
    }

    @Override
    public void showMonthlyMoreData(List<PlateNovels> list) {
        if (page == 0) {
            monthlyMoreAdapter.setNewData(list);
        } else {
            monthlyMoreAdapter.addData(list);
        }
        showContent();
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
